﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data.Common;
using System.Windows.Forms;

namespace BLL
{
	public class Generic
	{
		public static DbQuanLiTBDataContext DataContext;
		 static Generic()
		{

            //DataContext = new DbQuanLiTBDataContext("Data Source="+@MyConnectString.GetMyConnectString()+";Initial Catalog=QLTB;Integrated Security=True");
           DataContext = new DbQuanLiTBDataContext(MyConnectString.GetMyConnectString);
            // DataContext = new DbQuanLiTBDataContext("Data Source=liker-PC\\liker;Initial Catalog=QLTB;Integrated Security=True;");
		}

		public static bool Insert<T>(T e)
			where T : class
		{
            DataContext.GetTable<T>().InsertOnSubmit(e);
            
			DataContext.SubmitChanges();
			return true;
		}
		public static IEnumerable<T> GetAll<T>()
			where T : class
		{
            Reconnect();
			return DataContext.GetTable<T>().ToList();
		}
        public static T FindOneByCondition<T>(Func<T, bool> func)
            where T : class
        {
            return DataContext.GetTable<T>()
                     .ToList().Find(x => func(x) == true);
        }
		public static IEnumerable<T> FindByCondition<T>(Func<T, bool> func)
			where T : class
		{
			return DataContext.GetTable<T>()
					 .Where(func)
					 .ToList();
		}


		public static int DeleteByCondition<T>(Func<T, bool> func)
			where T : class
		{
			var ls = DataContext.GetTable<T>()
					 .Where(func)
					 .ToList();
            if (typeof(T).Name == "ThongBaoLoiThietBi")
            {
                QLThietBi.DeleteByMaTB((ls[0] as ThongBaoLoiThietBi).MaTB);
            }
			var r = DataContext.GetChangeSet().Deletes.Count;
			DataContext.GetTable<T>().DeleteAllOnSubmit(ls);
			DataContext.SubmitChanges();
			return r;
		}

		public static int EditByCondition<T>(Func<T, bool> func, Action<T> act)
			where T : class
		{
			DataContext.GetTable<T>()
								.Where(func)
								.ToList()
								.ForEach(act);
			int r = DataContext.GetChangeSet().Updates.Count;
			DataContext.SubmitChanges();
			return r;
		}

		public static void Commit()
		{
			DataContext.SubmitChanges();
		}

		public static bool TestConnection()
		{
			return DataContext.DatabaseExists();
		}
        public static void Reconnect()
        {
            DataContext = new DbQuanLiTBDataContext(MyConnectString.GetMyConnectString);
        }
		public static void ReCreateteDBML(string s)
		{
			DataContext = new DbQuanLiTBDataContext(s);
		}

		public static DbConnection GetConnectionInfo()
		{
			return DataContext.Connection;
		}
        public static void ReloadDataGridView<T>(DataGridView dgv)
            where T: class
        {
            dgv.DataSource = Generic.GetAll<T>();
        }
        public static void SelectDataGridviewRowTheoMa(DataGridView dgv, string ColumnName, object ma)
        {
            foreach(DataGridViewRow item in dgv.Rows)
            {
                item.Selected = false;
		        if(item.Cells[ColumnName].Value.ToString()==ma.ToString())
                {
                    item.Selected = true;
                    dgv.CurrentCell = item.Cells[0];
                    break;
                }

            }
        }
	}
}
